package com.kukudexin.quartz;

import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author: 酷酷的鑫
 * @Date: 2022/1/13 14:39
 * @description: 配置quartz定时任务的相关
 */
@Configuration
public class QUartzConfig {
    @Bean
    public JobDetail jobDetail1(){
        return JobBuilder.newJob(MyQuartzJob.class).storeDurably().build();
    }
    @Bean
    public Trigger trigger(){
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5)
                .repeatForever();
        return TriggerBuilder.newTrigger()
                .forJob(jobDetail1())
                .withSchedule(scheduleBuilder)
                .build();
    }

    @Bean
    public JobDetail jobDetail2(){
        Job quartzJob2 = (jobExecutionContext) -> {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("内部类quartzJob2----" + sdf.format(new Date()));
        };
        return JobBuilder.newJob(quartzJob2.getClass()).storeDurably().build();
    }

    @Bean
    public Trigger trigger2(){
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                //每2秒执行一次
                .withIntervalInSeconds(2)
                //永久重复，一直执行下去
                .repeatForever();
        return TriggerBuilder.newTrigger()
                .forJob(jobDetail2())
                .withSchedule(scheduleBuilder).build();
    }
}
